﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSMAEMTP:ObservableObject
    {

        MDOgeneral mod = new MDOgeneral();

        string codmat_mtp, codcol_mtp, codbar_mtp, _NOMCOLOR, _NOMMATERIAL;
        double eximtp_mtp, cansep_mtp;

        public string CODMAT_MTP
        {
            get { return codmat_mtp; }
            set { codmat_mtp = value; RaisePropertyChangedEvent("CODMAT_MTP"); }
        }

        public string CODCOL_MTP
        {
            get { return codcol_mtp; }
            set 
            {
                if (codcol_mtp != value) { CAMBIO = true; }
                    codcol_mtp = value;
                RaisePropertyChangedEvent("CODCOL_MTP");
            }
        }

        public string CODBAR_MTP
        {
            get { return codbar_mtp; }
            set 
            {
                if (codbar_mtp != value) { CAMBIO = true; }
                    codbar_mtp = value;
                RaisePropertyChangedEvent("CODBAR_MTP");
            }
        }

        public double EXIMTP_MTP
        {
            get { return eximtp_mtp; }
            set 
            {
                if (eximtp_mtp != value) { CAMBIO = true; }
                    eximtp_mtp = value;
                RaisePropertyChangedEvent("EXIMTP_MTP");
            }
        }
        
        public double CANSEP_MTP
        {
            get { return cansep_mtp; }
            set 
            {
                if (cansep_mtp != value) { CAMBIO = true; }
                    cansep_mtp = value;
                RaisePropertyChangedEvent("CANSEP_MTP");
            }
        }

        private Boolean _CAMBIO = false;
        public Boolean CAMBIO
        {
            get { return _CAMBIO; }
            set { _CAMBIO = value; RaisePropertyChangedEvent("CAMBIO"); }
        }
        private Boolean _EXISTE = false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
        }

        private Boolean _ELIMINAR = false;
        public Boolean ELIMINAR
        {
            get { return _ELIMINAR; }
            set
            {
                if (_ELIMINAR != value) { CAMBIO = true; }
                 _ELIMINAR = value;
                RaisePropertyChangedEvent("ELIMINAR");
            }
        }

        public string NOMCOLOR
        {
            get { return _NOMCOLOR; }
            set
            {
                if (_NOMCOLOR != value) { CAMBIO = true; }
                    _NOMCOLOR = value;
                RaisePropertyChangedEvent("NOMCOLOR");
            }
        }

        public string NOMMATERIAL
        {
            get { return _NOMMATERIAL; }
            set
            {
                if (_NOMMATERIAL != value) { CAMBIO = true; }
                    _NOMMATERIAL = value;
                RaisePropertyChangedEvent("NOMMATERIAL");
            }
        }

        public DataTable BuscarColor()
        {
            try
            {
                string query = "select distinct codcol_mtp as Codigo, nomcol_col as nombre from maemtp inner join maecolp on codcol_mtp=codcol_col where codmat_mtp='" + CODMAT_MTP + "' and codcol_mtp = '" + CODCOL_MTP + "'";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable BuscarColores()
        {
            try
            {
                string query = "select distinct codcol_mtp as Codigo, nomcol_col as nombre, eximtp_mtp as existencia from maemtp inner join maecolp on codcol_mtp=codcol_col where codmat_mtp='" + CODMAT_MTP + "'";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void ConsultaMaterial()
        {
            try
            {
                string query = "select a.codmat_mat, a.nommat_mat, b.codcol_mtp, c.nomcol_col, b.CODBAR_MTP, b.EXIMTP_MTP, b.CANSEP_MTP from maemat a inner join maemtp b on a.codmat_mat=b.codmat_mtp inner join maecolp c on b.codcol_mtp=c.codcol_col where a.codmat_mat='" + CODMAT_MTP + "' and b.codcol_mtp = '" + CODCOL_MTP + "'";
                DataTable val = mod.consulta(query);
                foreach (DataRow r in val.Rows)
                {
                    CODMAT_MTP = r["CODMAT_MAT"].ToString();
                    NOMMATERIAL = r["NOMMAT_MAT"].ToString();
                    CODCOL_MTP = r["CODCOL_MTP"].ToString();
                    NOMCOLOR = r["NOMCOL_COL"].ToString();
                    CODBAR_MTP = r["CODBAR_MTP"].ToString();
                    EXIMTP_MTP = Convert.ToDouble(r["EXIMTP_MTP"]);
                    CANSEP_MTP = Convert.ToDouble(r["CANSEP_MTP"]);
                    EXISTE = true;
                    CAMBIO = false;
                }

            }
            catch (Exception ex) { throw (ex); }
        }

        public List<CLSMAEMTP> ListarMatPri() 
        {
            try 
            {
                string query = "select CODMAT_MTP,(select NOMCOL_COL from MAECOLP where CODCOL_COL=CODCOL_MTP) as NOMCOLOR,CODCOL_MTP,CODBAR_MTP,EXIMTP_MTP,CANSEP_MTP from MAEMTP where CODMAT_MTP='" + CODMAT_MTP + "'";
                DataTable val = mod.consulta(query);
                List<CLSMAEMTP> MatPri = new List<CLSMAEMTP>();
                
                foreach(DataRow r in val.Rows)
                {
                    MatPri.Add(new CLSMAEMTP 
                    { 
                        CODMAT_MTP = r["CODMAT_MTP"].ToString(),
                        CODCOL_MTP = r["CODCOL_MTP"].ToString(),
                        NOMCOLOR = r["NOMCOLOR"].ToString(), 
                        CODBAR_MTP =  r["CODBAR_MTP"].ToString(),
                        EXIMTP_MTP = Double.Parse(r["EXIMTP_MTP"].ToString()),
                        CANSEP_MTP = Double.Parse(r["CANSEP_MTP"].ToString()),
                        EXISTE=true,
                        CAMBIO=false
                    });
                }
                return MatPri;

            }catch(Exception ex){ throw(ex);}
        }

        public int actualizar()
        {
            try 
            {
                string query = "update MAEMTP set CODBAR_MTP='"+CODBAR_MTP+"',EXIMTP_MTP="+EXIMTP_MTP+",CANSEP_MTP="+CANSEP_MTP+" where CODMAT_MTP='" + CODMAT_MTP + "' and CODCOL_MTP='" + CODCOL_MTP+"'";
                return mod.comando(query);

            }catch(Exception ex){ throw(ex);}
        }
        public int insertar() 
        {            
            try 
            {
                if (CODBAR_MTP == null)//FJRF - Validamos el codigo de barras que no venga nulo y si es nulo hacemos lo siguiente
                {
                    CODBAR_MTP = " ";//FJRF - Cargamos el dato con un espacio en blanco
                }
                string query = "insert into MAEMTP (CODMAT_MTP,CODCOL_MTP,CODBAR_MTP,EXIMTP_MTP,CANSEP_MTP) values ('"+CODMAT_MTP+"','"+CODCOL_MTP+"','"+CODBAR_MTP+"',"+EXIMTP_MTP+","+CANSEP_MTP+")";
                return mod.comando(query);

            }catch (Exception ex) { throw (ex); }
        }
        public int eliminar()
        {
            try
            {
                string query ="delete from  MAEMTP where CODMAT_MTP='" + CODMAT_MTP + "' and CODCOL_MTP='" + CODCOL_MTP+"'";
                return mod.comando(query);

            }
            catch (Exception ex) { throw (ex); }
        }
        public int eliMatMat()
        {
            try
            {

                string query = "delete from  MAEMTP where CODMAT_MTP='" + CODMAT_MTP + "'";
                return mod.comando(query);

            }
            catch (Exception ex) { throw (ex); }
        }
        public Boolean existe()
        {
            try
            {
                string query = "select count(*) from MAEMTP where CODMAT_MTP='" + CODMAT_MTP + "' and CODCOL_MTP='" + CODCOL_MTP + "'";
                return mod.consulta(query).Rows[0][0].ToString().Equals("0")?false:true;
            }
            catch (Exception ex) { throw (ex); }
        }
        public Boolean guardarMat(List<CLSMAEMTP> LisMateriap)
        {
            Boolean sw = true;
            try
            {
                for (int i = 0; i <= LisMateriap.Count - 1; i++)
                {
                    if(LisMateriap[i].CODCOL_MTP!=null){

                    if (LisMateriap[i].CAMBIO && LisMateriap[i].CODCOL_MTP!="")
                    {

                        if (LisMateriap[i].ELIMINAR == true && LisMateriap[i].EXISTE == true)
                        {
                            if (LisMateriap[i].eliminar() <= 0)
                            {
                                sw = false;
                            }
                        }
                        else
                        {
                            if (LisMateriap[i].ELIMINAR == false)
                            {
                                if (LisMateriap[i].existe() == true)
                                {
                                    if (LisMateriap[i].actualizar() <= 0)
                                    {

                                        sw = false;
                                    }
                                }
                                else
                                {
                                    if (LisMateriap[i].insertar() <= 0)
                                    {
                                        sw = false;
                                    }
                                }
                            }
                        }

                    }
                }
                }

            }
            catch (Exception ex) { throw (ex); }
            return sw;
        }
        public Boolean tieneExistencia(){
            try {
                string query = "select EXIMTP_MTP from MAEMTP where CODMAT_MTP='" + CODMAT_MTP + "' and CODCOL_MTP='" + CODCOL_MTP+"'";
                DataTable val = mod.consulta(query);
                if (val.Rows.Count > 0)
                {
                    if (Convert.ToInt32(val.Rows[0][0]) > 0) {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex) { throw(ex);}
        }

        public int SumarSep()
        {
            try
            {
                string query = "update maemtp set cansep_mtp = cansep_mtp + " + CANSEP_MTP + " where codmat_mtp='" + CODMAT_MTP + "' and codcol_mtp='" + CODCOL_MTP + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int RestarSep()
        {
            try
            {
                string query = "update maemtp set cansep_mtp = cansep_mtp - " + CANSEP_MTP + " where codmat_mtp='" + CODMAT_MTP + "' and codcol_mtp='" + CODCOL_MTP + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int SumarExi()
        {
            try
            {
                string query = "update maemtp set eximtp_mtp = eximtp_mtp + " + EXIMTP_MTP + " where codmat_mtp='" + CODMAT_MTP + "' and codcol_mtp='" + CODCOL_MTP + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int RestarExi()
        {
            try
            {
                string query = "update maemtp set eximtp_mtp = eximtp_mtp - " + EXIMTP_MTP + " where codmat_mtp='" + CODMAT_MTP + "' and codcol_mtp='" + CODCOL_MTP + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
